/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import pojo.DimensionMedia;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Roaa
 */
public class DimensionMediaDao {

    private Connection con;

    public DimensionMediaDao(Connection con){
        this.con=con;
    }

    //update data in dimension media table
    public boolean update(DimensionMedia mData) {
        int f = 0;
        boolean result = false;
        try {
            PreparedStatement stmt = con.prepareStatement("UPDATE dimensionmedia SET length=? , lengthby=? , width=? , widthby=? , depth=? , depthby=? , weight=? , weightby=? , prodcode=? , prodcodeby=? , mailformat=? , mailformatby=? , items=? , itemsby=? WHERE upc= ?");
            stmt.setString(15, mData.getUpc());
            stmt.setInt(1, mData.getLength());
            stmt.setShort(2, mData.getLengthBy());
            stmt.setInt(3, mData.getWidth());
            stmt.setShort(4, mData.getWidthBy());
            stmt.setInt(5, mData.getDepth());
            stmt.setShort(6, mData.getDepthBy());
            stmt.setInt(7, mData.getWeight());
            stmt.setShort(8, mData.getWeightBy());
            stmt.setString(9, mData.getProdCode());
            stmt.setShort(10, mData.getProdCodeBy());
            stmt.setString(11, mData.getMailFormat());
            stmt.setShort(12, mData.getMailFormatBy());
            stmt.setInt(13, mData.getItems());
            stmt.setShort(14, mData.getItemsBy());
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimensionDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    //insert data into media table
    public boolean insert(DimensionMedia mData) {
        int f = 0;
        boolean result = false;
        try {
            PreparedStatement stmt = con.prepareStatement("INSERT INTO dimensionmedia VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            stmt.setString(1, mData.getUpc());
            stmt.setInt(2, mData.getLength());
            stmt.setShort(3, mData.getLengthBy());
            stmt.setInt(4, mData.getWidth());
            stmt.setShort(5, mData.getWidthBy());
            stmt.setInt(6, mData.getDepth());
            stmt.setShort(7, mData.getDepthBy());
            stmt.setInt(8, mData.getWeight());
            stmt.setShort(9, mData.getWeightBy());
            stmt.setString(10, mData.getProdCode());
            stmt.setShort(11, mData.getProdCodeBy());
            stmt.setString(12, mData.getMailFormat());
            stmt.setShort(13, mData.getMailFormatBy());
            stmt.setInt(14, mData.getItems());
            stmt.setShort(15, mData.getItemsBy());
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimensionDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    //delete from media table
    public boolean delete(String upc) {
        int f = 0;
        boolean result = false;
        try {
            PreparedStatement stmt = con.prepareStatement("DELETE FROM dimensionmedia WHERE upc LIKE ?");
            stmt.setString(1, upc);
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimensionDataDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }
    
    //get All Data Using UPC
    public Vector<DimensionMedia> getByUPC(String upc){
        try {
            Vector<DimensionMedia> dmvect = new Vector<DimensionMedia>();
            PreparedStatement ps = con.prepareStatement("select * from dimensionmedia where upc = ? ");
            ps.setString(1, upc);
            ResultSet rset = ps.executeQuery();
            while (rset.next()) {
                DimensionMedia dm = new DimensionMedia();
                dm.setUpc(rset.getString(1));
                dm.setLength(rset.getInt(2));
                dm.setLengthBy(rset.getShort(3));
                dm.setWidth(rset.getInt(4));
                dm.setWidthBy(rset.getShort(5));
                dm.setDepth(rset.getInt(6));
                dm.setDepthBy(rset.getShort(7));
                dm.setWeight(rset.getInt(8));
                dm.setWeightBy(rset.getShort(9));
                dm.setProdCode(rset.getString(10));
                dm.setProdCodeBy(rset.getShort(11));
                dm.setMailFormat(rset.getString(12));
                dm.setMailFormatBy(rset.getShort(13));
                dm.setItems(rset.getInt(14));
                dm.setItemsBy(rset.getShort(15));
                dmvect.add(dm);
            }
            return dmvect;
        } catch (SQLException ex) {
            Logger.getLogger(ProviderDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
